<template>
  <view class="login-container">
		<view class="app_title">
			<text>AI-Notebook</text>
		</view>
    <view class="login-box">
      <uni-forms ref="form" :modelValue="formData" :rules="rules">
        <uni-forms-item label="账号" name="username">
          <uni-easyinput 
            type="text" 
            v-model="formData.username"
            placeholder="请输入账号"
            trim
          />
        </uni-forms-item>
        <uni-forms-item label="密码" name="password">
          <uni-easyinput 
            type="password" 
            v-model="formData.password"
            placeholder="请输入密码"
            trim
          />
        </uni-forms-item>
        <uni-forms-item>
          <uni-data-checkbox 
            v-model="formData.rememberMe"
            :localdata="rememberOptions"
            mode="list"
          />
        </uni-forms-item>
        <uni-popup ref="errorPopup" type="message">
          <uni-popup-message 
            type="error" 
            :message="errorMessage" 
            :duration="2000"
          />
        </uni-popup>
        <button 
          class="login-btn" 
          type="primary" 
          :loading="isLoading"
          @click="submitForm"
        >
          {{ isLoading ? '登录中...' : '登录' }}
        </button>

        <view class="third-party-login">
          <view class="divider">
            <view class="line"></view>
            <text class="text">或使用第三方登录</text>
            <view class="line"></view>
          </view>
          
          <view class="icons">
            <uni-icons 
              type="weixin" 
              size="30" 
              color="#07C160"
              @click="wechatLogin"
            />
            <uni-icons 
              type="qq" 
              size="30" 
              color="#12B7F5"
              @click="qqLogin"
            />
          </view>
        </view>

        <view class="register-link">
          没有账号？
					<text @click="goRegister">立即注册</text>
        </view>
      </uni-forms>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        username: '',
        password: '',
        rememberMe: []
      },
      isLoading: false,
      errorMessage: '',
      rememberOptions: [
        { value: true, text: '记住登录状态' }
      ],
      rules: {
        username: {
          rules: [{
            required: true,
            errorMessage: '请输入账号'
          }]
        },
        password: {
          rules: [{
            required: true,
            errorMessage: '请输入密码'
          }, {
            minLength: 6,
            maxLength: 20,
            errorMessage: '密码长度在6到20个字符之间'
          }]
        }
      }
    }
  },
  created() {
    const savedUsername = uni.getStorageSync('username')
    if (savedUsername) {
      this.formData.username = savedUsername
      this.formData.rememberMe = [true]
    }
  },
  methods: {
    // 模拟登陆的验证
    mockLogin() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          if (this.formData.username === 'admin' && this.formData.password === '123456') {
            resolve({ 
              success: true,
              message: '登录成功'
            })
          } else if(this.formData.username === '' || this.formData.password === ''){
            resolve({
              success: false,
              message: '账号和密码不能为空'
            })
          }else {
            resolve({ 
              success: false,
              message: '账号或密码错误'
            })
          }
        }, 250)
      })
    },
    async submitForm() {
      try {
        // 表单验证动画
        await this.$refs.form.validate()
        this.isLoading = true
        this.errorMessage = ''
        
        // 添加登录按钮加载动画
        this.$refs.form.$el.querySelector('.login-btn').classList.add('loading')
        
        // 模拟登录请求
        const res = await this.mockLogin()
        
        if (res.success) {
          // 登录成功处理
          uni.setStorageSync('isAuthenticated', true)
          uni.setStorageSync('lastLogin', Date.now())
          
          // 记住登录状态
          if (this.formData.rememberMe) {
            uni.setStorageSync('username', this.formData.username)
            uni.setStorageSync('rememberMe', true)
          } else {
            uni.removeStorageSync('username')
            uni.removeStorageSync('rememberMe')
          }
					console.log("登陆成功...") // 登陆成功的提示
					uni.switchTab({
						url: '/pages/home/index'
					})
        } else {
          // 登录失败处理
          this.errorMessage = res.message
          this.$refs.errorPopup.open()
          
          // 添加错误动画
          this.$refs.form.$el.classList.add('shake')
          setTimeout(() => {
            this.$refs.form.$el.classList.remove('shake')
          }, 500)
        }
      } catch (err) {
        // 错误处理
        this.errorMessage = err.message || '登录失败，请稍后重试'
        this.$refs.errorPopup.open()
        
        // 添加错误动画
        this.$refs.form.$el.classList.add('shake')
        setTimeout(() => {
          this.$refs.form.$el.classList.remove('shake')
        }, 500)
      } finally {
        this.isLoading = false
        this.$refs.form.$el.querySelector('.login-btn')?.classList?.remove('loading') // 移除加载动画
      }
    },
    
    wechatLogin() {
			console.log("微信登陆...");
			// 微信登录逻辑
    },
    
    qqLogin() {
			console.log("qq登录...")
			// qq登录逻辑
    },
    
		// 注册
    goRegister() {
      uni.navigateTo({
        url: '/pages/register/index'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app_title {
  padding: 30px;
  height: 100rpx;
  text-align: center;
  font-size: 60rpx;
  font-weight: bold;
  color: #1f92f099;
  margin: 60rpx 0 40rpx;
  text-shadow: 0 4px 8px rgba(100,181,246,0.15);
  animation: fadeInUp 0.8s ease;
}

.login-container {
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: radial-gradient(
    circle at 50% 0%,
    rgba(100,181,246,0.1) 0%,
    rgba(255,255,255,1) 100%
  );
  position: relative;
  overflow: hidden;
  animation: gradientAnimation 10s ease infinite;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(
      circle at 50% 50%,
      rgba(100,181,246,0.05) 0%,
      transparent 70%
    );
    z-index: 0;
    animation: float 6s ease-in-out infinite;
  }
  
  &::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 200px;
    background: linear-gradient(
      0deg,
      rgba(100,181,246,0.1) 0%,
      transparent 100%
    );
    z-index: 1;
    animation: wave 5s ease-in-out infinite;
  }

  .particles {
    position: absolute;
    width: 100%;
    height: 100%;
    overflow: hidden;
    z-index: 0;
    
    .particle {
      position: absolute;
      width: 4rpx;
      height: 4rpx;
      background: rgba(100,181,246,0.4);
      border-radius: 50%;
      animation: particleFloat 5s infinite ease-in-out;
      box-shadow: 0 0 8px rgba(100,181,246,0.2);
      
      @for $i from 1 through 50 {
        &:nth-child(#{$i}) {
          top: random(100) * 1%;
          left: random(100) * 1%;
          animation-duration: random(5) + 5s;
          animation-delay: random(5) + 0s;
          transform: scale(random(10) * 0.1 + 0.5);
        }
      }
    }
  }
}

.login-box {
  width: 90%;
  padding: 40rpx;
  height: auto;
  min-height: 400rpx;
  background: linear-gradient(
    145deg,
    rgba(255, 255, 255, 0.95) 0%,
    rgba(255, 255, 255, 0.98) 100%
  );
  border-radius: 32px;
  box-shadow: 
    0 4px 24px rgba(100,181,246,0.1),
    0 8px 48px rgba(100,181,246,0.05);
  border: 1px solid rgba(100,181,246,0.08);
  position: relative;
  z-index: 2;
  backdrop-filter: blur(16px);
  animation: fadeInUp 0.8s ease;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 32px;
    background: radial-gradient(
      circle at 50% 0%,
      rgba(100,181,246,0.05) 0%,
      transparent 80%
    );
    z-index: -1;
    opacity: 0.6;
    animation: float 6s ease-in-out infinite;
  }
  
  &:hover {
    transform: translateY(-4px);
    box-shadow: 
      0 8px 32px rgba(100,181,246,0.15),
      0 12px 64px rgba(100,181,246,0.08);
    border-color: rgba(100,181,246,0.12);
    
    &::before {
      opacity: 0.8;
    }
  }
}

.login-title {
  text-align: center;
  margin-bottom: 40rpx;
  color: $uni-text-color;
}

.login-btn {
  margin-top: 10rpx;
  margin-bottom: 10rpx;
  position: relative;
  overflow: hidden;
  background: linear-gradient(145deg, #65c9ed8b 0%, #c6def1c0 100%);
  border: none;
  box-shadow: 0 4px 16px rgba(100,181,246,0.2);
  border-radius: 20rpx;
  font-weight: 600;
  font-size: 36rpx;
  line-height: 60rpx;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  transform-style: preserve-3d;
  color: #fff;
  text-shadow: 0 2px 4px rgba(0,0,0,0.1);
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
      90deg,
      transparent,
      rgba(255,255,255,0.3),
      transparent
    );
    transition: all 0.5s ease;
  }
  
  &::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle, rgba(255,255,255,0.3) 10%, transparent 10.01%);
    transform: scale(0);
    transition: transform 0.5s, opacity 0.5s;
    pointer-events: none;
  }
  
  &:hover {
    transform: translateY(-2px) scale(1.02);
    box-shadow: 0 6px 24px rgba(100,181,246,0.4);
    background: linear-gradient(145deg, #90caf9 0%, #64b5f6 100%);
    
    &::before {
      left: 100%;
    }
  }
  
  &:active {
    transform: scale(0.96);
    box-shadow: 0 2px 8px rgba(100,181,246,0.2);
    
    &::after {
      transform: scale(2);
      opacity: 0;
    }
  }
  
  &.loading {
    &::before {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      width: 24rpx;
      height: 24rpx;
      border: 3rpx solid #fff;
      border-top-color: transparent;
      border-radius: 50%;
      animation: spin 0.8s linear infinite;
      transform: translate(-50%, -50%);
    }
    
    &::after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: radial-gradient(circle, rgba(255,255,255,0.2) 10%, transparent 10.01%);
      animation: ripple 1.2s infinite;
    }
  }
}

.login-btn:hover {
  background: linear-gradient(135deg, #64b5f6, #90caf9);
  box-shadow: 0 6px 24px rgba(100,181,246,0.4);
  transform: translateY(-2px);
}

.login-btn:hover::before {
  left: 100%;
}

.login-btn:active {
  transform: scale(0.94);
  box-shadow: 0 1px 4px rgba(100,181,246,0.2);
  opacity: 0.9;
}

.login-btn.loading {
  background: linear-gradient(135deg, #64b5f6, #90caf9);
  opacity: 0.9;
  cursor: not-allowed;
}

.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.login-btn.loading {
  position: relative;
}

.login-btn.loading::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20rpx;
  height: 20rpx;
  border: 2rpx solid #fff;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  transform: translate(-50%, -50%);
}

.third-party-login {
  margin-top: 80rpx;
  text-align: center;
  background: rgba(255,255,255,0.98);
  padding: 40rpx;
  border-radius: 28rpx;
  border: 1px solid rgba(100,181,246,0.06);
}

.divider {
  display: flex;
  align-items: center;
  margin: 40rpx 0;
}

.divider .line {
  flex: 1;
  height: 2px;
  background: linear-gradient(90deg, transparent, rgba(100,181,246,0.3), transparent);
  box-shadow: 0 2px 4px rgba(100,181,246,0.1);
  transition: all 0.3s ease;
}

.divider:hover .line {
  background: linear-gradient(90deg, transparent, rgba(100,181,246,0.4), transparent);
  box-shadow: 0 4px 8px rgba(100,181,246,0.15);
}

.divider .text {
  margin: 0 20rpx;
  color: rgba(100,181,246,0.95);
  font-size: 28rpx;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(100,181,246,0.15);
  padding: 8rpx 16rpx;
  border-radius: 12rpx;
  background: rgba(100,181,246,0.05);
}

.divider:hover .text {
  color: #64b5f6;
  text-shadow: 0 4px 8px rgba(100,181,246,0.2);
  background: rgba(100,181,246,0.08);
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.icons {
  display: flex;
  justify-content: center;
  gap: 80rpx;
  margin-top: 40rpx;
  padding: 32rpx;
}

.icons uni-icons {
  width: 60rpx;
  height: 60rpx;
  color: #64b5f6;
}

.register-link {
  margin-top: 80rpx;
  text-align: center;
  color: rgba(100,181,246,0.9);
  font-size: 32rpx;
  animation: fadeInUp 1s ease;
  padding: 20rpx;
  border-radius: 24rpx;
  background: rgba(255,255,255,0.95);
  backdrop-filter: blur(12px);
}

.register-link text {
  color: #64b4f6d7;
  font-weight: 600;
  cursor: pointer;
  position: relative;
  transition: all 0.3s ease;
  padding: 8rpx 16rpx;
  border-radius: 12rpx;
  background: rgba(100,181,246,0.05);
}

.register-link text::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(100,181,246,0.1);
  border-radius: 12rpx;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.register-link text::after {
  content: '';
  position: absolute;
  bottom: -2rpx;
  left: 50%;
  width: 0;
  height: 2rpx;
  background: #64b5f6;
  transition: all 0.3s ease;
  transform: translateX(-50%);
}

.register-link text:hover {
  color: #90caf9;
  background: rgba(100,181,246,0.08);
}

.register-link text:hover::before {
  opacity: 1;
}

.register-link text:hover::after {
  width: 100%;
  background: #90caf9;
}

::v-deep {
.uni-forms-item__label {
  padding: 0 !important;
  color: rgba(100,181,246,0.95);
  font-size: 30rpx;
  font-weight: 600;
  margin-bottom: 16rpx;
  letter-spacing: 0.5rpx;
  text-shadow: 0 2px 4px rgba(100,181,246,0.1);
  transition: all 0.3s ease;
}

.uni-forms-item__label:hover {
  color: #64b5f6;
  text-shadow: 0 4px 8px rgba(100,181,246,0.2);
}

.uni-forms-item__content {
  margin-top: 20rpx;
}

::v-deep .uni-easyinput__content {
  border: 1px solid rgba(100,181,246,0.15);
  border-radius: 20rpx;
  transition: all 0.3s ease;
  box-shadow: 
    0 2px 6px rgba(100,181,246,0.05),
    inset 0 1px 2px rgba(255,255,255,0.1);
  background: linear-gradient(
    145deg,
    rgba(255,255,255,0.96) 0%,
    rgba(255,255,255,0.98) 100%
  );
  backdrop-filter: blur(16px);
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
      90deg,
      transparent,
      rgba(100,181,246,0.08),
      transparent
    );
    transition: all 0.6s ease;
  }

  &::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 2rpx;
    background: linear-gradient(90deg, #64b5f6, #90caf9);
    transform: scaleX(0);
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  }
  
  &:hover {
    border-color: rgba(100,181,246,0.25);
    box-shadow: 
      0 4px 12px rgba(100,181,246,0.08),
      inset 0 1px 2px rgba(255,255,255,0.15);
    
    &::before {
      left: 100%;
    }
  }
  
  &:focus-within {
    border-color: rgba(100,181,246,0.4);
    box-shadow: 
      0 6px 16px rgba(100,181,246,0.12),
      inset 0 1px 2px rgba(255,255,255,0.2);
    animation: inputFocus 0.3s ease;
    
    &::after {
      transform: scaleX(1);
      background: linear-gradient(90deg, #90caf9, #64b5f6);
    }
  }
}

::v-deep .uni-easyinput__content:hover {
  border-color: rgba(100,181,246,0.4);
  box-shadow: 0 4px 12px rgba(100,181,246,0.12);
  background: rgba(255,255,255,1);
  
  &::before {
    left: 100%;
  }
  
  &::after {
    transform: scaleX(1);
  }
}

::v-deep .uni-easyinput__content:focus-within {
  border-color: #64b5f6;
  box-shadow: 0 6px 16px rgba(100,181,246,0.2);
  background: rgba(255,255,255,1);
  animation: inputFocus 0.3s ease;
  
  &::after {
    transform: scaleX(1);
    background: linear-gradient(90deg, #90caf9, #64b5f6);
  }
}

@keyframes inputFocus {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.02);
  }
  100% {
    transform: scale(1);
  }
}

::v-deep .uni-easyinput__content:hover {
  border-color: rgba(100,181,246,0.4);
  box-shadow: 0 4px 12px rgba(100,181,246,0.12);
  background: rgba(255,255,255,1);
  
  &::before {
    left: 100%;
  }
}

::v-deep .uni-easyinput__content:focus-within {
  border-color: #64b5f6;
  box-shadow: 0 6px 16px rgba(100,181,246,0.2);
  background: rgba(255,255,255,1);
  animation: inputFocus 0.5s ease;
  
  &::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 2rpx;
    background: linear-gradient(90deg, #64b5f6, #90caf9);
    animation: inputLine 0.5s ease;
  }
}

@keyframes inputFocus {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.02);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes inputLine {
  0% {
    transform: scaleX(0);
  }
  100% {
    transform: scaleX(1);
  }
}

.uni-data-checkbox {
  margin: 20rpx 0;
  padding: 10rpx;
  border-radius: 12rpx;
  background: rgba(100,181,246,0.05);
}
}
</style>
